package com.seanLab.tool.TagSuggestModel.ExpandRank;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.seanLab.dto.SuggestModelArticleDto;
import com.seanLab.tool.TagSuggestModel.ExpandRank.common.ConfigIO;
import org.thunlp.misc.WeightString;

import java.io.*;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.Logger;

public class TestDemo {
    private static Logger LOG = Logger.getAnonymousLogger();
    private static final Type POST_TYPE = new TypeToken<List<SuggestModelArticleDto>>() {}.getType();
    private static final String DATASET_PATH = "dataset";
    private static final String DATASET = "sina.article_xiaobo2.json";
    private static final String WORK_PATH = "workingDir";
    private static final String CONFIG = "num_tags=10;norm=all_log;k=5;minwordfreq=10;mintagfreq=10";

    public static void main(String[] args) throws IOException {
        List<SuggestModelArticleDto> data = new Gson().fromJson(
                new JsonReader(new FileReader(DATASET_PATH + File.separator + DATASET)), POST_TYPE);
        TrainExpandRank trainer = new TrainExpandRank();
        trainer.train(data, WORK_PATH, ConfigIO.configFromString(CONFIG));

        ExpandRankKE suggestor = new ExpandRankKE();
        suggestor.loadModel(WORK_PATH + File.separator + "model_22029");
        suggestor.setConfig(ConfigIO.configFromString(CONFIG));

        doSuggest(suggestor, WORK_PATH + File.separator + "TestArticle.txt",
                WORK_PATH + File.separator + "TestOutput.txt");
    }

    public static void doSuggest(ExpandRankKE suggester, String article_path, String output_path)  throws IOException {

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(
                                article_path),
                        "UTF-8"));

        String title = reader.readLine();
        String content = reader.readLine();

        SuggestModelArticleDto p = new SuggestModelArticleDto();     //a trick that avoid error in extract  KeywordPost
        p.setTitle(title);
        p.setContent(content);

        long startTime = System.currentTimeMillis();
        List<WeightString> tags = suggester.suggest(p);
        long endTime = System.currentTimeMillis();
        LOG.info("Time: " + String.valueOf(endTime - startTime));

        BufferedWriter outTag = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(output_path),
                "UTF-8"));
        int cnt =  0;
        for (WeightString ws : tags) {
            outTag.write(ws.text + " " + ws.weight);
            LOG.info(ws.text + " " + ws.weight);
            ++ cnt;
            if (cnt>10) break;
            outTag.newLine();
            outTag.flush();
        }
    }
}
